GBML manual / Maths

angle_tan

The tangent of angle (degrees, 0 = east, counterclockwise) - sine divided by cosine.

Syntax

angle_tan(angle) -> real

Arguments

NameType
anglereal

Returns

real

Description

The tangent of angle - degrees, 0 is east, counterclockwise - which is just angle_sin divided by angle_cos. Where angle_sin/angle_cos answer "how far in this direction," angle_tan answers "how steep is this direction" - the ratio between the up/down half and the sideways half, the same thing a slope or a grade on a road sign means.

Example

// A laser sight: how far sideways the shot drifts for every pixel it travels forward, along
// whatever direction this is currently aiming.
var slope = angle_tan(direction);
var sideways_at_80 = slope * 80;

If direction is a shallow angle, slope is small and the shot barely drifts sideways over 80 pixels of forward travel; aimed closer to straight up or down, slope grows quickly - which is exactly the shape a real tangent has, and exactly why it needs its own answer for what happens AT straight up/down, below.

Straight up or down has no finite answer - and what this does instead

At exactly 90 and 270 degrees, angle_cos is 0 - dividing by it has no real answer (an infinitely steep line has no "how far sideways per pixel forward"). Rather than crash there, this saturates at plus or minus 64 - big enough that nothing on a GBA screen could ever tell it apart from true infinity - and the sign still comes out correct on either side of the asymptote (approaching 90 from below returns +64; from just above, -64). That saturation starts within about 0.9 degrees of either asymptote, not only at the exact degree - a real limit of the fixed-point maths involved, disclosed here rather than left to be found by surprise.

Cost

The one function in this group with a genuine divide in it - real numbers have no hardware division on this CPU, so angle_tan is the most expensive function in the Maths group. Reach for angle_sin/angle_cos directly when only one of the two is actually needed.

On the GBA

Costs one division on top of angle_sin/angle_cos’s own cost - real division has no hardware instruction on this CPU, so this is the single most expensive function in the Maths group. Undefined at 90 and 270 degrees (where cosine is exactly 0) - rather than crashing there, the result saturates at plus or minus 64 (real’s own division breaks down below a divisor of 1/64, a fixed-point-library fact, not an arbitrary choice - see the engine header for the full account), already reached within about 0.9 degrees of either asymptote and big enough nothing on a GBA screen could tell it from true infinity.

See also: angle_sinangle_cos